home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 039a / dflat8.zip / CALENDAR.C < prev    next >
Text File  |  1991-09-30  |  4KB  |  169 lines

  1. /* ------------- calendar.c ------------- */
  2. #include "dflat.h"
  3.  
  4. #ifndef TURBOC
  5.  
  6. #define CALHEIGHT 17
  7. #define CALWIDTH 33
  8.  
  9. static int DyMo[] = {31,28,31,30,31,30,31,31,30,31,30,31};
  10. static struct tm ttm;
  11. static int dys[42];
  12. static WINDOW Cwnd;
  13.  
  14. static void FixDate(void)
  15. {
  16.     /* ---- adjust Feb for leap year ---- */
  17.     DyMo[1] = (ttm.tm_year % 4) ? 28 : 29;
  18. #ifndef BCPP
  19.     /* --- a bug in the Borland C++ mktime function prohibits this --- */
  20.     ttm.tm_mday = min(ttm.tm_mday, DyMo[ttm.tm_mon]);
  21. #endif
  22. }
  23.  
  24. /* ---- build calendar dates array ---- */
  25. static void BuildDateArray(void)
  26. {
  27.     int offset, dy = 0;
  28.     memset(dys, 0, sizeof dys);
  29.     FixDate();
  30.     /* ----- compute the weekday for the 1st ----- */
  31.     offset = ((ttm.tm_mday-1) - ttm.tm_wday) % 7;
  32.     if (offset < 0)
  33.         offset += 7;
  34.     if (offset)
  35.         offset = (offset - 7) * -1;
  36.     /* ----- build the dates into the array ---- */
  37.     for (dy = 1; dy <= DyMo[ttm.tm_mon]; dy++)
  38.         dys[offset++] = dy;
  39. }
  40.  
  41. static void CreateWindowMsg(WINDOW wnd)
  42. {
  43.     int x, y;
  44.     DrawBox(wnd, 1, 2, CALHEIGHT-4, CALWIDTH-4);
  45.     for (x = 5; x < CALWIDTH-4; x += 4)
  46.         DrawVector(wnd, x, 2, CALHEIGHT-4, FALSE);
  47.     for (y = 4; y < CALHEIGHT-3; y+=2)
  48.         DrawVector(wnd, 1, y, CALWIDTH-4, TRUE);
  49. }
  50.  
  51. static void DisplayDates(WINDOW wnd)
  52. {
  53.     int week, day;
  54.     char dyln[10];
  55.     int offset;
  56.     char banner[CALWIDTH-1];
  57.     char banner1[30];
  58.  
  59.     SetStandardColor(wnd);
  60.     PutWindowLine(wnd, "Sun Mon Tue Wed Thu Fri Sat", 2, 1);
  61.     memset(banner, ' ', CALWIDTH-2);
  62.     strftime(banner1, 16, "%B, %Y", &ttm);
  63.     offset = (CALWIDTH-2 - strlen(banner1)) / 2;
  64.     strcpy(banner+offset, banner1);
  65.     strcat(banner, "    ");
  66.     PutWindowLine(wnd, banner, 0, 0);
  67.     BuildDateArray();
  68.     for (week = 0; week < 6; week++)    {
  69.         for (day = 0; day < 7; day++)    {
  70.             int dy = dys[week*7+day];
  71.             if (dy == 0)
  72.                 strcpy(dyln, "   ");
  73.             else    {
  74.                 if (dy == ttm.tm_mday)
  75.                     sprintf(dyln, "%c%c%c%2d %c",
  76.                         CHANGECOLOR,
  77.                         SelectForeground(wnd)+0x80,
  78.                         SelectBackground(wnd)+0x80,
  79.                         dy, RESETCOLOR);
  80.                 else
  81.                     sprintf(dyln, "%2d ", dy);
  82.             }
  83.             SetStandardColor(wnd);
  84.             PutWindowLine(wnd, dyln, 2 + day * 4, 3 + week * 2);
  85.         }
  86.     }
  87. }
  88.  
  89. static int KeyboardMsg(WINDOW wnd, PARAM p1)
  90. {
  91.     switch ((int)p1)    {
  92.         case PGUP:
  93.             if (ttm.tm_mon == 0)    {
  94.                 ttm.tm_mon = 12;
  95.                 ttm.tm_year--;
  96.             }
  97.             ttm.tm_mon--;
  98.             FixDate();
  99.             mktime(&ttm);
  100.             DisplayDates(wnd);
  101.             return TRUE;
  102.         case PGDN:
  103.             ttm.tm_mon++;
  104.             if (ttm.tm_mon == 12)    {
  105.                 ttm.tm_mon = 0;
  106.                 ttm.tm_year++;
  107.             }
  108.             FixDate();
  109.             mktime(&ttm);
  110.             DisplayDates(wnd);
  111.             return TRUE;
  112.         default:
  113.             break;
  114.     }
  115.     return FALSE;
  116. }
  117.  
  118. static int CalendarProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  119. {
  120.     switch (msg)    {
  121.         case CREATE_WINDOW:
  122.             DefaultWndProc(wnd, msg, p1, p2);
  123.             CreateWindowMsg(wnd);
  124.             return TRUE;
  125.         case KEYBOARD:
  126.             if (KeyboardMsg(wnd, p1))
  127.                 return TRUE;
  128.             break;
  129.         case PAINT:
  130.             DefaultWndProc(wnd, msg, p1, p2);
  131.             DisplayDates(wnd);
  132.             return TRUE;
  133.         case COMMAND:
  134.             if ((int)p1 == ID_HELP)    {
  135.                 DisplayHelp(wnd, "Calendar");
  136.                 return TRUE;
  137.             }
  138.             break;
  139.         case CLOSE_WINDOW:
  140.             Cwnd = NULL;
  141.             break;
  142.         default:
  143.             break;
  144.     }
  145.     return DefaultWndProc(wnd, msg, p1, p2);
  146. }
  147.  
  148. void Calendar(WINDOW pwnd)
  149. {
  150.     if (Cwnd == NULL)    {
  151.         time_t tim = time(NULL);
  152.         ttm = *localtime(&tim);
  153.         Cwnd = CreateWindow(PICTUREBOX,
  154.                     "Calendar",
  155.                     -1, -1, CALHEIGHT, CALWIDTH,
  156.                     NULL, pwnd, CalendarProc,
  157.                     SHADOW     |
  158.                     MINMAXBOX  |
  159.                     CONTROLBOX |
  160.                     MOVEABLE   |
  161.                     HASBORDER
  162.         );
  163.     }
  164.     SendMessage(Cwnd, SETFOCUS, TRUE, 0);
  165. }
  166.  
  167. #endif
  168.  
  169.